package com.zyk.scaffold.gateway.properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Component
@RefreshScope
@ConfigurationProperties(prefix = "open-api", ignoreUnknownFields = false)
public class OpenApiProperties {
    private List<App> app = new ArrayList<>();

    public List<String> getUrisByAppId(String appId) {
        Optional<App> appOptional = getAppByAppId(appId);
        App app = appOptional.orElse(null);
        if(app == null){
            return Collections.emptyList();
        }
        List<String> uris = app.getUris();
        if(uris == null){
            return Collections.emptyList();
        }
        return uris;
    }

    public Optional<String> getAppKeyByAppId(String appId) {
        Optional<App> appOptional = getAppByAppId(appId);
        App app = appOptional.orElse(null);
        if(app == null){
            return Optional.empty();
        }
        return Optional.ofNullable(app.getAppKey());
    }

    public Optional<App> getAppByAppId(String appId) {
        return this.app.stream().filter(item -> StringUtils.equals(appId, item.getAppId())).findFirst();
    }

    public List<App> getApp() {
        return app;
    }

    public void setApp(List<App> app) {
        this.app = app;
    }

    public static class App {

        /**
         * appId
         */
        private String appId;

        /**
         * appName
         */
        private String appName;

        /**
         * tenantCode
         */
        private String tenantCode;

        /**
         * tenantName
         */
        private String tenantName;
        /**
         * appKey
         */
        private String appKey;

        /**
         * 限流
         */
        private List<Limit> limits;

        /**
         * 时间校验开关
         */
        private Boolean enableTimeOut = Boolean.TRUE;

        /**
         * 超时时间
         */
        private Long timeout = 6000L;

        /**
         * 时间偏移
         */
        private Long timeoffset = 0L;

        /**
         * URL列表
         */
        private List<String> uris = new ArrayList<>();

        public String getAppId() {
            return appId;
        }

        public void setAppId(String appId) {
            this.appId = appId;
        }

        public String getAppKey() {
            return appKey;
        }

        public void setAppKey(String appKey) {
            this.appKey = appKey;
        }

        public List<Limit> getLimits() {
            return limits;
        }

        public void setLimits(List<Limit> limits) {
            this.limits = limits;
        }

        public Long getTimeoffset() {
            return timeoffset;
        }

        public void setTimeoffset(Long timeoffset) {
            this.timeoffset = timeoffset;
        }

        public Long getTimeout() {
            return timeout;
        }

        public void setTimeout(Long timeout) {
            this.timeout = timeout;
        }

        public List<String> getUris() {
            return uris;
        }

        public void setUris(List<String> uris) {
            this.uris = uris;
        }

        public String getAppName() {
            return appName;
        }

        public void setAppName(String appName) {
            this.appName = appName;
        }

        public String getTenantCode() {
            return tenantCode;
        }

        public void setTenantCode(String tenantCode) {
            this.tenantCode = tenantCode;
        }

        public String getTenantName() {
            return tenantName;
        }

        public void setTenantName(String tenantName) {
            this.tenantName = tenantName;
        }

        public Boolean getEnableTimeOut() {
            return enableTimeOut;
        }

        public void setEnableTimeOut(Boolean enableTimeOut) {
            this.enableTimeOut = enableTimeOut;
        }
    }

    public static class Limit {

        /**
         * 匹配规则
         */
        private String matching = "/**";

        /**
         * 时间窗
         */
        private int timeWindow = 60;

        /**
         * 时间窗允许的次数
         * 0:截止状态
         * -1:失效状态
         * 大于0正常状态
         */
        private int frequency = -1;

        public String getMatching() {
            return matching;
        }

        public void setMatching(String matching) {
            this.matching = matching;
        }

        public int getTimeWindow() {
            return timeWindow;
        }

        public void setTimeWindow(int timeWindow) {
            this.timeWindow = timeWindow;
        }

        public int getFrequency() {
            return frequency;
        }

        public void setFrequency(int frequency) {
            this.frequency = frequency;
        }
    }
}
